<?php

namespace app\admin\controller\post;

use app\common\controller\Backend;

class Comment extends Backend
{
    protected $model = null;
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->PostModel = model("Post.Post");
        $this->CategoryModel = model("Post.Category");
        $this->CollectModel = model("Post.Collect");
        $this->CommentModel = model("Post.Comment");
        $this->BusinessModel = model("Business.Business");

        $statuslist = [
            '0' => '未采纳',
            '1' => '已采纳',
        ];
        $this->assign([
            'statusList'=>$statuslist,
        ]);
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->CommentModel
                ->with(['business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count(); //查询总数

            $list = $this->CommentModel
                ->with(['business'])
                ->where($where)
                ->order($sort,  $order)
                ->limit($offset, $limit)
                ->select(); //查询数据
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function post()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->PostModel
                ->with(['business', 'category','accepted'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count(); //查询总数

            $list = $this->PostModel
                ->with(['business', 'category','accepted'])
                ->where($where)
                ->order($sort,  $order)
                ->limit($offset, $limit)
                ->select(); //查询数据
            // echo $this->OrderModel->getlastSql();
            // exit;
            // halt(collection($list)->toArray());
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function comment()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->CommentModel
                ->with(['business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count(); //查询总数

            $list = $this->CommentModel
                ->with(['business'])
                ->where($where)
                ->order($sort,  $order)
                ->limit($offset, $limit)
                ->select(); //查询数据
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
 
    public function add()
    {
        if ($this->request->isAjax()) 
        {
            $params=$this->request->param('row/a');
            
            if(empty($params['pid_text']))
            {
                $params['pid']=0;
            }
            // halt($params);

            $result=$this->CommentModel->validate('common/Post/Comment')->save($params);
            if($result === FALSE)
            {
                $this->error($this->CommentModel->getError());
                exit;
            }
            $this->success();
            exit;
        }
        return $this->view->fetch();
    }
    public function edit($ids = null)
    {
        $row=$this->CommentModel->with(['business','post'])->find($ids);
    
        if(!$row)
        {
            $this->error(__('No Results were found'));
            exit;
        }
        // 上一级的评论
        $lastbusid=$this->CommentModel->with(['business','post'])->where(['comment.id'=>$row['pid']])->find();
        $row['lastbusid']=$lastbusid;

        $this->assign("row", $row);

        if ($this->request->isAjax()) 
        {
            $params=$this->request->param('row/a');
            
            if(empty($params['pid_text']))
            {
                $params['pid']=0;
            }
            $params['id']=$ids;
            $result=$this->CommentModel->validate('common/Post/Comment')->isUpdate(true)->save($params);
            if($result === FALSE)
            {
                $this->error($this->CommentModel->getError());
                exit;
            }
            $this->success();
            exit;
        }
        return $this->view->fetch();
    }

    public function del($ids = null)
    {
        $ids = !empty($ids) ? explode(',', $ids) : [];
        $row = $this->CommentModel->column('id');
        $DelList=[];
        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error('帖子分类不存在');
                exit;
            }
            // 递归
            $delids=$this->roll($item);
            // 将多个数组合并
            $DelList=array_merge($DelList,$delids);
        }

        $result=$this->CommentModel->destroy($DelList);
        if($result ===FALSE)
        {
            $this->error($this->CommentModel->getError());
            exit;
        }
        $this->success();
        exit;
    }

    // 递归删除
    public function roll($pid,$list=[])
    {
        if($this->request->isAjax());
        {
            $rows=$this->CommentModel->where(['pid'=>$pid])->select();
            $list[]=$pid;

            if($rows)
            {
                foreach($rows as $item)
                {
                    // 递归
                    $list=$this->roll($item['id'],$list);
                }
            }
            return $list;
        }
    }
}